home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / net / readline-2.0mg.tar.gz / readline-2.0mg.tar / readline-2.0mg / isearch.c < prev    next >
C/C++ Source or Header  |  1994-11-20  |  10KB  |  383 lines

  1. /* **************************************************************** */
  2. /*                                    */
  3. /*            I-Search and Searching                */
  4. /*                                    */
  5. /* **************************************************************** */
  6.  
  7. /* Copyright (C) 1987,1989 Free Software Foundation, Inc.
  8.  
  9.    This file contains the Readline Library (the Library), a set of
  10.    routines for providing Emacs style line input to programs that ask
  11.    for it.
  12.  
  13.    The Library is free software; you can redistribute it and/or modify
  14.    it under the terms of the GNU General Public License as published by
  15.    the Free Software Foundation; either version 1, or (at your option)
  16.    any later version.
  17.  
  18.    The Library is distributed in the hope that it will be useful, but
  19.    WITHOUT ANY WARRANTY; without even the implied warranty of
  20.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  21.    General Public License for more details.
  22.  
  23.    The GNU General Public License is often shipped with GNU software, and
  24.    is generally kept in a file called COPYING or LICENSE.  If you do not
  25.    have a copy of the license, write to the Free Software Foundation,
  26.    675 Mass Ave, Cambridge, MA 02139, USA. */
  27. #define READLINE_LIBRARY
  28.  
  29. #if defined (HAVE_CONFIG_H)
  30. #  include "config.h"
  31. #endif
  32.  
  33. #include <stdio.h>
  34.  
  35. #if defined (HAVE_UNISTD_H)
  36. #  include <unistd.h>
  37. #endif
  38.  
  39. #include "memalloc.h"
  40. #include "readline.h"
  41. #include "history.h"
  42.  
  43. #define STREQ(a, b)    (((a)[0] == (b)[0]) && (strcmp ((a), (b)) == 0))
  44. #define STREQN(a, b, n)    (((a)[0] == (b)[0]) && (strncmp ((a), (b), (n)) == 0))
  45.  
  46. /* Variables imported from other files in the readline library. */
  47. extern Keymap _rl_keymap;
  48. extern HIST_ENTRY *saved_line_for_history;
  49. extern int rl_line_buffer_len;
  50. extern int rl_point, rl_end;
  51. extern char *rl_line_buffer;
  52.  
  53. extern char *xmalloc (), *xrealloc ();
  54.  
  55. static int rl_search_history ();
  56.  
  57. /* Last line found by the current incremental search, so we don't `find'
  58.    identical lines many times in a row. */
  59. static char *prev_line_found;
  60.  
  61. /* Search backwards through the history looking for a string which is typed
  62.    interactively.  Start with the current line. */
  63. rl_reverse_search_history (sign, key)
  64.      int sign;
  65.      int key;
  66. {
  67.   return (rl_search_history (-sign, key));
  68. }
  69.  
  70. /* Search forwards through the history looking for a string which is typed
  71.    interactively.  Start with the current line. */
  72. rl_forward_search_history (sign, key)
  73.      int sign;
  74.      int key;
  75. {
  76.   return (rl_search_history (sign, key));
  77. }
  78.  
  79. /* Display the current state of the search in the echo-area.
  80.    SEARCH_STRING contains the string that is being searched for,
  81.    DIRECTION is zero for forward, or 1 for reverse,
  82.    WHERE is the history list number of the current line.  If it is
  83.    -1, then this line is the starting one. */
  84. static void
  85. rl_display_search (search_string, reverse_p, where)
  86.      char *search_string;
  87.      int reverse_p, where;
  88. {
  89.   char *message;
  90.  
  91.   message = xmalloc (1 + (search_string ? strlen (search_string) : 0) + 30);
  92.   *message = '\0';
  93.  
  94. #if defined (NOTDEF)
  95.   if (where != -1)
  96.     sprintf (message, "[%d]", where + history_base);
  97. #endif /* NOTDEF */
  98.  
  99.   strcat (message, "(");
  100.  
  101.   if (reverse_p)
  102.     strcat (message, "reverse-");
  103.  
  104.   strcat (message, "i-search)`");
  105.  
  106.   if (search_string)
  107.     strcat (message, search_string);
  108.  
  109.   strcat (message, "': ");
  110.   rl_message ("%s", message, 0);
  111.   free (message);
  112.   rl_redisplay ();
  113. }
  114.  
  115. /* Search through the history looking for an interactively typed string.
  116.    This is analogous to i-search.  We start the search in the current line.
  117.    DIRECTION is which direction to search; >= 0 means forward, < 0 means
  118.    backwards. */
  119. static int
  120. rl_search_history (direction, invoking_key)
  121.      int direction;
  122.      int invoking_key;
  123. {
  124.   /* The string that the user types in to search for. */
  125.   char *search_string;
  126.  
  127.   /* The current length of SEARCH_STRING. */
  128.   int search_string_index;
  129.  
  130.   /* The amount of space that SEARCH_STRING has allocated to it. */
  131.   int search_string_size;
  132.  
  133.   /* The list of lines to search through. */
  134.   char **lines, *allocated_line = (char *)NULL;
  135.  
  136.   /* The length of LINES. */
  137.   int hlen;
  138.  
  139.   /* Where we get LINES from. */
  140.   HIST_ENTRY **hlist = history_list ();
  141.  
  142.   register int i = 0;
  143.   int orig_point = rl_point;
  144.   int orig_line = where_history ();
  145.   int last_found_line = orig_line;
  146.   int c, done = 0, found, failed, sline_len;
  147.  
  148.   /* The line currently being searched. */
  149.   char *sline;
  150.  
  151.   /* Offset in that line. */
  152.   int line_index;
  153.  
  154.   /* Non-zero if we are doing a reverse search. */
  155.   int reverse = (direction < 0);
  156.  
  157.   /* Create an arrary of pointers to the lines that we want to search. */
  158.   maybe_replace_line ();
  159.   if (hlist)
  160.     for (i = 0; hlist[i]; i++);
  161.  
  162.   /* Allocate space for this many lines, +1 for the current input line,
  163.      and remember those lines. */
  164.   lines = (char **)xmalloc ((1 + (hlen = i)) * sizeof (char *));
  165.   for (i = 0; i < hlen; i++)
  166.     lines[i] = hlist[i]->line;
  167.  
  168.   if (saved_line_for_history)
  169.     lines[i] = saved_line_for_history->line;
  170.   else
  171.     {
  172.       /* Keep track of this so we can free it. */
  173.       allocated_line = xmalloc (1 + strlen (rl_line_buffer));
  174.       strcpy (allocated_line, &rl_line_buffer[0]);
  175.       lines[i] = allocated_line;
  176.     }
  177.  
  178.   hlen++;
  179.  
  180.   /* The line where we start the search. */
  181.   i = orig_line;
  182.  
  183.   /* Initialize search parameters. */
  184.   search_string = xmalloc (search_string_size = 128);
  185.   *search_string = '\0';
  186.   search_string_index = 0;
  187.   prev_line_found = (char *)0;        /* XXX */
  188.  
  189.   /* Normalize DIRECTION into 1 or -1. */
  190.   direction = (direction >= 0) ? 1 : -1;
  191.  
  192.   rl_display_search (search_string, reverse, -1);
  193.  
  194.   sline = rl_line_buffer;
  195.   sline_len = strlen (sline);
  196.   line_index = rl_point;
  197.  
  198.   found = failed = 0;
  199.   while (!done)
  200.     {
  201.       Function *f = (Function *)NULL;
  202.  
  203.       /* Read a key and decide how to proceed. */
  204.       c = rl_read_key ();
  205.  
  206.       /* Hack C to Do What I Mean. */
  207.       if (_rl_keymap[c].type == ISFUNC)
  208.     {
  209.       f = _rl_keymap[c].function;
  210.  
  211.       if (f == rl_reverse_search_history)
  212.         c = reverse ? -1 : -2;
  213.       else if (f == rl_forward_search_history)
  214.         c =  !reverse ? -1 : -2;
  215.     }
  216.  
  217.       switch (c)
  218.     {
  219.     case ESC:
  220.       done = 1;
  221.       continue;
  222.  
  223.     case -1:
  224.       if (!search_string_index)
  225.         continue;
  226.       else
  227.         {
  228.           if (reverse)
  229.         --line_index;
  230.           else
  231.         {
  232.           if (line_index != sline_len)
  233.             ++line_index;
  234.           else
  235.             ding ();
  236.         }
  237.         }
  238.       break;
  239.  
  240.       /* switch directions */
  241.     case -2:
  242.       direction = -direction;
  243.       reverse = (direction < 0);
  244.       break;
  245.  
  246.     case CTRL ('G'):
  247.       strcpy (rl_line_buffer, lines[orig_line]);
  248.       rl_point = orig_point;
  249.       rl_end = strlen (rl_line_buffer);
  250.       rl_clear_message ();
  251.       free (allocated_line);
  252.       free (lines);
  253.       return 0;
  254.  
  255.     default:
  256.       if (CTRL_CHAR (c) || META_CHAR (c) || c == RUBOUT)
  257.         {
  258.           rl_execute_next (c);
  259.           done = 1;
  260.           continue;
  261.         }
  262.       else
  263.         {
  264.           /* Add character to search string and continue search. */
  265.           if (search_string_index + 2 >= search_string_size)
  266.         {
  267.           search_string_size += 128;
  268.           search_string = xrealloc (search_string, search_string_size);
  269.         }
  270.           search_string[search_string_index++] = c;
  271.           search_string[search_string_index] = '\0';
  272.           break;
  273.         }
  274.     }
  275.  
  276.       found = failed = 0;
  277.       while (1)
  278.     {
  279.       int limit = sline_len - search_string_index + 1;
  280.  
  281.       /* Search the current line. */
  282.       while (reverse ? (line_index >= 0) : (line_index < limit))
  283.         {
  284.           if (STREQN(search_string, sline + line_index, search_string_index))
  285.           {
  286.             found++;
  287.             break;
  288.           }
  289.           else
  290.         line_index += direction;
  291.         }
  292.       if (found)
  293.         break;
  294.  
  295.       /* Move to the next line, but skip new copies of the line
  296.          we just found and lines shorter than the string we're
  297.          searching for. */
  298.       do
  299.         {
  300.           /* Move to the next line. */
  301.           i += direction;
  302.  
  303.           /* At limit for direction? */
  304.           if (reverse ? (i < 0) : (i == hlen))
  305.         {
  306.           failed++;
  307.           break;
  308.         }
  309.  
  310.           /* We will need these later. */
  311.           sline = lines[i];
  312.           sline_len = strlen (sline);
  313.         }
  314.       while ((prev_line_found && STREQ (prev_line_found, lines[i])) ||
  315.          (search_string_index > sline_len));
  316.  
  317.       if (failed)
  318.         break;
  319.  
  320.       /* Now set up the line for searching... */
  321.       if (reverse)
  322.         line_index = sline_len - search_string_index;
  323.       else
  324.         line_index = 0;
  325.     }
  326.  
  327.       if (failed)
  328.     {
  329.       /* We cannot find the search string.  Ding the bell. */
  330.       ding ();
  331.       i = last_found_line;
  332.       continue;         /* XXX - was break */
  333.     }
  334.  
  335.       /* We have found the search string.  Just display it.  But don't
  336.      actually move there in the history list until the user accepts
  337.      the location. */
  338.       if (found)
  339.     {
  340.       int line_len;
  341.  
  342.       prev_line_found = lines[i];
  343.       line_len = strlen (lines[i]);
  344.  
  345.       if (line_len >= rl_line_buffer_len)
  346.         rl_extend_line_buffer (line_len);
  347.  
  348.       strcpy (rl_line_buffer, lines[i]);
  349.       rl_point = line_index;
  350.       rl_end = line_len;
  351.       last_found_line = i;
  352.       rl_display_search (search_string, reverse, (i == orig_line) ? -1 : i);
  353.     }
  354.     }
  355.  
  356.   /* The searching is over.  The user may have found the string that she
  357.      was looking for, or else she may have exited a failing search.  If
  358.      LINE_INDEX is -1, then that shows that the string searched for was
  359.      not found.  We use this to determine where to place rl_point. */
  360.  
  361.   /* First put back the original state. */
  362.   strcpy (rl_line_buffer, lines[orig_line]);
  363.  
  364.   /* Free the search string. */
  365.   free (search_string);
  366.  
  367.   if (last_found_line < orig_line)
  368.     rl_get_previous_history (orig_line - last_found_line);
  369.   else
  370.     rl_get_next_history (last_found_line - orig_line);
  371.  
  372.   /* If the string was not found, put point at the end of the line. */
  373.   if (line_index < 0)
  374.     line_index = strlen (rl_line_buffer);
  375.   rl_point = line_index;
  376.   rl_clear_message ();
  377.  
  378.   free (allocated_line);
  379.   free (lines);
  380.  
  381.   return 0;
  382. }
  383.